import { CSys, SketchFrame } from '@amcax/base';
import { ExpressionValue } from '@app-base/types';
import {
  BufferGeometry,
  Camera,
  Geometry,
  Group,
  Material,
  Scene,
  Vector2tuple,
  Vector3,
  WebGLRenderer,
} from 'three-legacy';
export type DimCallback = (
  status: 'focus' | 'blur',
  val?: number,
  originVal?: number,
  valParam?: string,
) => void;
export type LineKey =
  | 'line'
  | 'line1'
  | 'line2'
  | 'line3'
  | 'line4'
  | 'line5'
  | 'arcLine';
export type LocalOriginType = 'center' | 'start' | 'end';
export type DimensionLinear = {
  // 没有id说明是创建中的Dimension
  id?: string;
  lineVertices?: Vector2tuple[][];
  dimConstraint?: AnnotationType;
  point1?: Vector3;
  point2?: Vector3;
  pointArrow2?: Vector3;
  pointArrow1?: Vector3;
  lineLength?: number;
  sketchFrame?: SketchFrame;
  labelPos?: Vector3;
  // 相对为位置
  relPos?: Vector2tuple[];
  // 线段id
  curveIds?: string[];
  label?: number;
  labelValue?: number;
  param?: string;
  localOriginType?: LocalOriginType;
  callback?: DimCallback;
};

export type DiameterAttrs = {
  // 没有id说明是创建中的Dimension
  id?: string;
  dimConstraint?: AnnotationType;
  // 线段id
  curveIds?: string[];
  center?: Vector3;
  radius?: number;
  color?: number;
  startPoint?: Vector3;
  sketchFrame?: SketchFrame;
  labelPos?: Vector3;
  // 相对为位置
  relPos?: Vector2tuple[];
  label?: number;
  curveType?: string;
  callback?: DimCallback;
  param?: string;
};
export type QuadrantType = 1 | 2 | 3 | 4;
export type DimAngleAttrs = {
  // 没有id说明是创建中的Dimension
  id?: string;
  action?: 'create' | 'update';
  dimConstraint?: AnnotationType;
  lineVertices?: Vector2tuple[][];
  // 线段id
  curveIds?: string[];
  faceCSys?: CSys; // 局部坐标系对象
  sketchFrame?: SketchFrame;
  labelPos?: Vector3;
  // 相对为位置
  relPos?: Vector2tuple[];
  label?: number;
  param?: string;
  callback?: DimCallback;
};
export enum EllipseState {
  major = 'marjor',
  minor = 'minor',
}
export type EllipseType = EllipseState.major | EllipseState.minor;
export type EllipseAttrs = {
  // 没有id说明是创建中的Dimension
  id?: string;
  dimConstraint?: AnnotationType;
  center?: Vector3 | Vector2tuple; // 本地坐标
  major?: number;
  minor?: number;
  param?: string;
  majorDir?: Vector2tuple;
  curveIds?: string[];
  labelPos?: Vector3;
  relPos?: Vector2tuple[];
  state?: EllipseType;
  callback?: DimCallback;
};

export enum AnnotationType {
  DimensionAnno = 1, // 尺寸,对应直线
  DistanceAnno = 2, // 距离,需要传两个对象
  DistanceXAnno = 3, // 水平距离,一条直线或两个对象
  DistanceYAnno = 4, // 竖直距离,一条直线或两个对象
  AngleAnno = 5, // 角度,两个对象
  RadiusAnno = 6, // 半径,圆或圆弧
  MajorRadiusAnno = 7, // 长轴半径
  MinorRadiusAnno = 8, // 短轴半径
}

export type FunctionOnBeforeRender = (
  renderer: WebGLRenderer,
  scene: Scene,
  camera: Camera,
  geometry: Geometry | BufferGeometry,
  material: Material,
  group: Group,
) => void;

export const DimColors = {
  normal: {
    line: 0x101010,
    text: '#101010',
    mask: 'transparent',
  },
  hover: {
    line: 0xbcbcbd,
    text: '#000000',
    mask: '#bcbcbd',
  },
  select: {
    line: 0x22b8cf,
    text: '#000000',
    mask: '#22b8cf',
  },
  preview: {
    line: 0xb6b6b6,
    text: '#333333',
    mask: '#ffffff',
  },
  disable: {
    line: 0x8b8b8b,
    text: '#8b8b8b',
    mask: '#ffffff',
  },
};

export const extendsLine = 6; // 线延伸长度
export const extendsArc = 0.01; // 弧延伸长度
export const minusArc = 0.02; // 弧延较少
export const lineWidth = 1; // 线宽

export type AnnotationCreateParams = {
  curve: string[];
  type: AnnotationType;
  value: ExpressionValue;
  points: Vector2tuple[];
};

export type AnnotationUpdateParams = Partial<AnnotationCreateParams> & {
  annotation: string;
};
